package com.lucenly.card.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lucenly.card.R;
import com.lucenly.card.base.ActivityManager;

public class UpDataDialog2 extends Dialog{
    ImageView iv_updata,iv_exit;

    public UpDataDialog2(@NonNull final Activity context, final boolean isCancel) {
        super(context, R.style.alert_dialog);
        setContentView(R.layout.dialog_updata2);
        iv_exit = findViewById(R.id.iv_exit);
        iv_updata = findViewById(R.id.iv_updata);
        initView(context,isCancel);
        iv_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                ActivityManager.getInstance().appExit(context);
            }
        });
        iv_updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToMarket(context,context.getPackageName());
            }
        });

    }
    public static void goToMarket(Context context, String packageName) {
        Uri uri = Uri.parse("market://details?id=" + packageName);
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            context.startActivity(goToMarket);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initView(Context context,boolean isCancel) {
        LinearLayout lLayout_bg = (LinearLayout) findViewById(R.id.lLayout_bg);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        setCanceledOnTouchOutside(false);
        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));
        setCancelable(isCancel);
    }
}
